package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Customer;
import com.example.demo.entity.ResponseResult;
import com.example.demo.mapper.CustomerMapper;
import com.example.demo.service.CustomerService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
        implements CustomerService {
    @Override
    public ResponseResult getList() {
        List<Customer> customers = list();

        if(customers.isEmpty()){
            return ResponseResult.ok("没有相关顾客信息");
        }
        return ResponseResult.ok(customers);
    }

    /**
     * 根据id查询顾客
     * @param id
     * @return
     */
    @Override
    public ResponseResult getCustomerById(Integer id) {
        Customer customer = getById(id);
        if(customer == null){
            return ResponseResult.ok("没有这个顾客信息");
        }
        return ResponseResult.ok(customer);
    }

    /**
     * 根据顾客名称查询
     * @param name
     * @return
     */
    @Override
    public ResponseResult getCustomerByName(String name) {
        List<Customer> customers = list(new QueryWrapper<Customer>().eq("Customer_name", name));
        if(customers == null){
            return ResponseResult.ok("没有对应的顾客信息");
        }
        return ResponseResult.ok(customers);
    }

    @Override
    public ResponseResult saveCustomer(Customer customer) {
        if(customer == null){
            return ResponseResult.fail("请输入正确的顾客信息");
        }

        boolean isSuccess = save(customer);
        if(!isSuccess){
            return ResponseResult.fail("新增失败");
        }

        return ResponseResult.ok("新增成功");
    }

    /**
     * 顾客信息更新
     * @param customer
     * @return
     */
    @Override
    public ResponseResult updateCustomer(Customer customer) {
        if(customer == null){
            return ResponseResult.fail("请输入正确的顾客信息");
        }
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(customer.getName())){
            wrapper.eq("Customer_name",customer.getName());
        }
        if(customer.getId() != null){
            wrapper.eq("Customer_id",customer.getId());
        }
        boolean isSuccess = update(customer, wrapper);
        if(!isSuccess){
            return ResponseResult.fail("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }

    /**
     * 根据id删除顾客
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(String id) {
        if(id == null){
            return ResponseResult.fail("请输入正确的顾客编号");
        }
        boolean isSuccess = remove(new QueryWrapper<Customer>().eq("Customer_id", id));
        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    @Override
    public ResponseResult deleteByName(String name) {
        if(StrUtil.isBlank(name)){
            return ResponseResult.fail("请输入正确的顾客名称");
        }
        boolean isSuccess = remove(new QueryWrapper<Customer>().eq("Customer_name", name));
        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }
}
